package com.starblink.android.common.web

import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.common.databinding.ActivityGuangWebBinding
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CheckUtil
import com.starblink.web.util.AndroidBug5497Workaround
import com.starblink.web.util.WebUtil

/**
 * H5通用加载容器
 * 2023-03-27 by 五木
 */
@Route(path = RoutePage.Web.PAGE_GLOBAL_H5)
class GuangWebActivity : GuangWebBaseActivity<ActivityGuangWebBinding, BaseViewModel>(){

    @JvmField
    @Autowired(name = RoutePage.Web.ARG_TITLE)
    var mWebTitle: String? = null

    @JvmField
    @Autowired(name = RoutePage.Web.ARG_URL)
    var mWebUrl: String = ""

    @JvmField
    @Autowired(name = RoutePage.Web.ARG_USECACHE)
    var ifUseCache: Boolean = true

    @JvmField
    @Autowired(name = RoutePage.Web.ARG_STRICTMODE)
    var ifStrictMode: Boolean = false

    @JvmField
    @Autowired(name = RoutePage.Web.SPM_CNT)
    var spmCnt: String? = null

    override fun initViewBinding(): ActivityGuangWebBinding {
        return ActivityGuangWebBinding.inflate(layoutInflater)
    }

    //使用singleTask模式放开
//    override fun onNewIntent(intent: Intent?) {
//        super.onNewIntent(intent)
//        intent?.let {
//            if(!CheckUtil.isEmpty(intent.getStringExtra("mWebtitle"))) {
//                mWebtitle = intent.getStringExtra("mWebtitle")
//            }
//            mWeburl=intent.getStringExtra("mWeburl")
//        }
//        initTitles()
//        loadUrl()
//    }


    override fun initData() {
        super.initData()
        //埋点数据前置位
        if(!CheckUtil.isEmpty(spmCnt)){
            //h5传过了的前置位埋点
            GTrackerAssistUtils.previousEvent=spmCnt
        }
        if(WebUtil.isGuangUrl(mWebUrl)) {
            //只能自己的链接额外增加参数
            if (!CheckUtil.isEmpty(GTrackerAssistUtils.previousEvent) && !mWebUrl.contains("spmCnt=")) {
                mWebUrl += if (!mWebUrl.contains("?")) {
                    "?spmCnt=" + GTrackerAssistUtils.previousEvent
                } else {
                    "&spmCnt=" + GTrackerAssistUtils.previousEvent
                }
            }
        }
        initWebView(titleBar=viewBinding.titleBar,viewGroup=viewBinding.flContent,pbWebViewBar=viewBinding.pbWebViewBar,mWebUrl=mWebUrl,mWebTitle=mWebTitle,ifUseCache=ifUseCache,ifStrictMode=ifStrictMode)
        //webView软键盘挡住输入框 会导致页面展示内容不完全
        AndroidBug5497Workaround.assistActivity(viewBinding.rootContent)
    }
}